<?php
/**轻测数据图表化
 * Created by PhpStorm.
 * User: Auser
 * Date: 2017/1/17
 * Time: 10:55
 */
namespace App\Admin\Controllers;
use App;
//use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Chart\Bar;
use Encore\Admin\Widgets\Chart\Doughnut;
use Encore\Admin\Widgets\Chart\Pie;
use Encore\Admin\Widgets\Chart\PolarArea;
use Encore\Admin\Widgets\Collapse;
use Encore\Admin\Widgets\Form;
use TestingXjbHelper;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\InfoBox;
use Illuminate\Http\Request;
use WechatUserService;
use TestingDetailService;
use DecreaseEchartHelper;
use TestingConfig;
use UserRecordService;
class DecreaseEchartsController extends Controller
{

    public function index(Request $request){
        return Admin::content(function (Content $content) use($request){
            $tid=empty($request->input('tid'))? 21 : $request->input('tid');
            switch ((int)$tid){
                case 18:
                    $title = "S-轻体测评-玩健身";
                    break;
                case 19:
                    $title = "S-14轻体测评";
                    break;
                default:
                    $title = "S-轻体测评";
                    break;
            }
            $content->header($title);
            $content->description("注: 1、开始时间与截止时间为空时，直接点解提交，默认统计一周数据；2、为提高加载速度，请尽量选择时间范围在20天之内");
            $form = new Form();
            $url = $request->url();
            $url = $url."/show?tid=".$tid;
            $form->action($url);
            $form->dateTimeRange('date_time_start', 'date_time_end', '时间范围');
            $content->row(new Box('数据统计', $form));
        });
    }

    public function indexShow(Request $request)
    {
        $tid=empty($request->input('tid'))? 21 : $request->input('tid');
        switch ((int)$tid){
            case 18:
                $title = "S-轻体测评-玩健身";
                break;
            case 19:
                $title = "S-14轻体测评";
                break;
            default:
                $title = "S-轻体测评";
                break;
        }

        //$start_time = date('Y-m-d H:i:s', strtotime('-7 days'));
        //$end_time = date("Y-m-d H:i:s",time());
        $default_start_time = date('Y-m-d H:i:s', strtotime('-7 days'));
        $default_end_time = date("Y-m-d H:i:s",time());
        $start_time = empty($request->input('date_time_start')) ? $default_start_time : $request->input('date_time_start');
        $end_time = empty($request->input('date_time_end')) ? $default_end_time : $request->input('date_time_end');
        if($start_time < "2017-02-15 22:00:00") {
            $start_time = "2017-02-15 22:00:00";
        }
        return Admin::content(function (Content $content) use($tid,$title,$start_time,$end_time){
            $content->header($title);
            $content->description("数据统计: ".$start_time." 至 ".$end_time);
            $datas = DecreaseEchartHelper::getData($tid,$start_time,$end_time);
            $content->row(function (Row $row) use($datas){
                $data = $datas['visit'];
                //$data = DecreaseEchartHelper::visit($tid);
                $row->column(3, new InfoBox('平均测试时间(s)', 'book', 'yellow','',  $data['avgtime']));
                $row->column(3, new InfoBox('PV(人数)', 'users', 'aqua', '', $data['pv']));
                $row->column(3, new InfoBox('今日UV(人数)', 'users', 'aqua', '', $data['uv']));
                $row->column(3, new InfoBox('今日IP(人数)', 'users', 'aqua', '', $data['ip']));
            });

            $content->row(function (Row $row) use($datas){
                $row->column(6, function (Column $column) use($datas){
                    //$data = DecreaseEchartHelper::sleep($tid);
                    $data = $datas['sleep'];
                    $doughnut = new Doughnut([
                        ['晚上11点后睡觉', $data['over']],
                        ['晚上11点前睡觉', $data['normal']],
                    ]);
                    $column->append((new Box('睡眠状态(%)', $doughnut))->removable()->collapsable()->style('info'));
                });

               $row->column(6, function (Column $column)use($datas) {
                    //$data = DecreaseEchartHelper::physical($tid);
                   $data = $datas['physical'];
                    $pie = new Pie([
                        ['腰腹部最为肥胖', $data['first']], ['上身瘦下身肥', $data['second']], ['腰细但胸部和臀部肥厚', $data['three']], ['全身粗壮，无明显区域性', $data['four']],
                        ['非腰腹部的局部脂肪堆积',  $data['five']],
                    ]);
                    $column->append((new Box('身材状态(%)', $pie))->removable()->collapsable()->style('info'));
                });

                $row->column(6, function (Column $column)use($datas) {
                    //$sex = DecreaseEchartHelper::sex($tid);
                    $sex = $datas['sex'];
                    $polarArea = new PolarArea([
                        ['男', $sex[1]],
                        ['女', $sex[0]],
                    ]);
                    $column->append((new Box('性别比例(%)', $polarArea))->removable()->collapsable()->style('info'));
                });

                $row->column(6, function (Column $column) use($datas){
                    //$data = DecreaseEchartHelper::status($tid);
                    $data = $datas['status'];
                    $collapse = new Collapse();
                    $bar = new Bar(
                        ["糖尿病","高血压","高血脂","心血管疾病","肝肾疾病","多囊卵巢综合症","没有以上疾病"],
                        [
                            ['second', [$data['first'],$data['second'],$data['three'],$data['four'],$data['five'],$data['six'],$data['seven']]],
                        ]
                    );
                    $collapse->add('健康程度(人数)', $bar);
                    $column->append($collapse);
                });

                $row->column(6, function (Column $column) use($datas){
                    //$data = DecreaseEchartHelper::fatScale($tid);
                    $data = $datas['fatScale'];
                    $collapse = new Collapse();
                    $bar = new Bar(
                        ["肥胖","","超重","","体重正常","","体重过低"],
                        [
                            ['second', [$data['first'],0,$data['second'],0,$data['three'],0,$data['four']]],
                        ]
                    );
                    $collapse->add('肥胖状态(人数)', $bar);
                    $column->append($collapse);
                });

                $row->column(6, function (Column $column) use($datas){
                    //$data = DecreaseEchartHelper::obesityIndex($tid);
                    $data =$datas['obesityIndex'];
                    $collapse = new Collapse();
                    $bar = new Bar(
                        $data['describe'],
                        [
                            ['First',$data['index']],
                        ]
                    );
                    $collapse->add('肥胖指数(人数)', $bar);
                    $column->append($collapse);
                });

                $row->column(6, function (Column $column)use($datas) {
                    //$data = DecreaseEchartHelper::obesityStatus($tid);
                    $data = $datas['obesityStatus'];
                    $pie = new Pie([
                        ['单位食堂', $data['first']],
                        ['基本自己做饭或者亲爱的家人做饭', $data['second']],
                        ['一天两顿以上外卖或下馆子', $data['three']],
                        ['以上情况都不符', $data['four']],
                    ]);
                    $column->append((new Box('肥胖人群饮食状态(%)', $pie))->removable()->collapsable()->style('info'));
                });

                $row->column(6, function (Column $column)use($datas) {
                   // $data = DecreaseEchartHelper::intestineHealth($tid);
                    $data = $datas['intestineHealth'];
                    $pie = new Pie([
                        ['习惯性便秘', $data['first']],
                        ['偶尔便秘', $data['second']],
                        ['我的肠道很健康', $data['three']],
                    ]);
                    $column->append((new Box('肥胖人群肠道健康状况(%)', $pie))->removable()->collapsable()->style('info'));
                });

                $row->column(6, function (Column $column)use($datas) {
                   // $data = DecreaseEchartHelper::obesityDiet($tid);
                    $data = $datas['obesityDiet'];
                    $pie = new Pie([
                        ['经常无法吃早餐', $data['first']],
                        ['三餐中晚餐最丰富', $data['second']],
                        ['偏好重口味，比如重油、甜的、辣的或咸的食物', $data['three']],
                        ['一周总有3次以上吃到撑', $data['four']],
                        ['经常喝甜饮料，一周3次以上',  $data['five']],
                        ['常备各种包装类零食，如糕点、卤制品等', $data['six']],
                        ['蔬菜并不是每一顿饭都有', $data['seven']],
                        ['总是忍不住吃街边的小吃，一周总会吃3次以上', $data['eight']],
                        ['以上都不符合', $data['nine']],
                    ]);
                    $column->append((new Box('肥胖人群饮食习惯(%)', $pie))->removable()->collapsable()->style('info'));
                });

                $row->column(6, function (Column $column)use($datas) {
                    //$data = DecreaseEchartHelper::resistance($tid);
                    $data = $datas['resistance'];
                    $pie = new Pie([
                        ['减少肉类摄入', $data['first']],
                        ['吃很多蔬菜', $data['second']],
                        ['坚持运动', $data['three']],
                        ['晚上11点以前睡觉', $data['four']],
                        ['每天留出1小时减肥',  $data['five']],
                        ['什么都能做到', $data['six']],
                        ['以上我都做不到', $data['seven']],
                    ]);
                    $column->append((new Box('肥胖人群减重最大阻力(%)', $pie))->removable()->collapsable()->style('info'));
                });

                $row->column(12, function (Column $column) use($datas){
                    //$province =  DecreaseEchartHelper:: province();
                    //$data = DecreaseEchartHelper:: data($tid);
                    $province = $datas['province'];
                    $provinceData = $datas['provinceData'];
                    $collapse = new Collapse();
                    $bar = new Bar(
                        $province,
                        [
                            ['First', $provinceData],
                        ]
                    );
                    $collapse->add('省份分布(%)', $bar);
                    $column->append($collapse);
                });

            });
        });
    }

}